<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Config extends Admin_Controller {
		private $db;

		public function __construct(){  
			parent::__construct();
			$this->db = $this->load->database('admin',true);
			if($this->admin_info['admin_type']==0){
				$this->alertMsg("普通管理员无该权限!",$this->base_url);
			}
		}

	     public function access(){
			$sql	= "select * from cfd_admin_access order by access_order asc,sid asc";
			$query	= $this->db->query($sql);
			$result	= $query->result_array();
			//获取所有权限节点
			$access[] = "{id:0, name:'所有权限', open:true, checked:false}";
			if(!empty($result)) foreach($result as $items){
				$open = ($items['depth']<0)?'true':'false';
				$checked = 'false';
				$sid=$items['sid'];
				$access_url=$items['access_url'];
				$target=$items['target'];
				$access[] = "{id:".$sid.", pId:".$items['pid'].", name:'".$items['access_name']."', open:".$open.", checked:".$checked.",access_order:".$items['access_order'].", status:".$items['status'].",access_url:'".$access_url."',target:'$target', isNew:false}";				
			}
			$accessStr = implode(',',$access);
			$accessData= array(
				'accessStr'=>$accessStr,
				'title'=>'权限设置',
			);
			$this->data['page']			=	'config_access';
			$this->data['page_data']	=	$accessData;
			$this->load->view('layout',$this->data);
	     }

		//角色管理
		public function role(){
			$sql	= "select * from cfd_admin_user order by admin_id desc";
			$query	= $this->db->query($sql);
			$result	= $query->result_array();
			$this->data['page']			=	'config_role';
			$this->data['page_data']	=	$result;
		    $this->load->view('layout',$this->data);
		}

		public function doAccess(){
			$post	=	$this->input->post();
	     	$model	=	json_decode($this->fisker_decode_v2($post['model']),true);//模组
	     	$menu	=	json_decode($this->fisker_decode_v2($post['menu']) ,true) ;//菜单
	     	$act	=	json_decode($this->fisker_decode_v2($post['act'])  ,true) ;//具体操作

	     	$model_arr	=	$this->saveAccess($model, array());//保存之后的模组数组
	     	$menu_arr	=	$this->saveAccess($menu,$model_arr);//保存之后的菜单数组
	     	$act_arr	=	$this->saveAccess($act,$menu_arr);
	     	if(!empty($model_arr)||!empty($menu_arr)||!empty($act_arr)){
	     		echo '{"success":',1,',"arr":'.json_encode(array($model_arr,$menu_arr,$act_arr)).'}';
	     	}else{
	     		echo '{"success":',0,'}';
	     	}
	     }

		 /**
		 * 编辑角色
		 */
		public function doRole(){
			$get		=	$this->input->get();
			$admin_id	=	$get['admin_id'];
			$user_arr	=	array();

			if(empty($admin_id)){//新增权限判断
				$title="角色添加";
				if($platform==1){
					$accessStr=$this->admin_editpage(array());
				}else{
					$accessStr=$this->admin_editpage(array(),true,false);
				}
				$arr=getAllRow("admin_accessgroup","platform='$platform' and status=1", "","", $this->admin_conn);
				$page_arr=array(
						"title"=>$title,
						"sid"=>$this->sid,
						'accessStr'=>$accessStr,
						"user"=>$user_arr,
						"group"=>$arr
				);
				$this->data['page']='admin_edit';
				$this->data['page_data']=$page_arr;
			}else{//权限判断
				$this->get_assess($this->sid);
				$title="角色编辑";
				$where = $where1." and admin_id='$admin_id'";
				$table='admin_admin';
				$user_arr =getOneRow($table, $where, "", $this->admin_conn);
				if(empty($user_arr)){//用户不存在
					 $page_arr=array(
						"url"=>"/index.php?c=global&a=admin&sid=".$this->sid,
						"msg"=>"该用户不存在",
						"time"=>3
					 );
					 
					 $this->load->my_view('alert_index',$page_arr,$this->data);//渲染模板
					 $this->data['page']='alert_index';
					 $this->data['page_data']=$page_arr;
				}else{
			// 		         		if($user_arr['admin_type']==1 && $this->user_info['admin_id']!=$admin_id){
			// 		         			$this->load->my_alert($this->data,'不可编辑其他超级用户!',"/index.php?c=global&a=admin&sid=".$this->sid,2);
			// 		         			return;
			// 		         		}
					$user_platform=$user_arr['platform'];
					$arr=getAllRow("admin_accessgroup","platform='$user_platform' and status=1", "","", $this->admin_conn);
					if($user_arr['admin_accessgroup']!=0){
						$tempid=$user_arr['admin_accessgroup'];
						$user_group=getOneRow("admin_accessgroup", "group_id=$tempid","", $this->admin_conn);
						$user_access=$user_group['group_access'];
						$islistAll = false;
					}else{
						$user_access=$user_arr['admin_access'];
						$islistAll = true;
					}
					if($platform==1){
						$allAccess=true;
					}else{
						$allAccess=false;
					}
					$userAcccess = array();
					$tmp = explode('|',$user_access);
					if(!empty($tmp)) foreach($tmp as $items){
						$tmp2 = explode(',',$items);
						$userAcccess = array_merge($userAcccess,$tmp2);
					}
					$accessStr=$this->admin_editpage($userAcccess,$islistAll,$allAccess);
					$page_arr=array(
							"title"=>$title,
							"sid"=>$this->sid,
							'accessStr'=>$accessStr,
							"user"=>$user_arr,
							"group"=>$arr
					);
					$this->data['page']='admin_edit';
					$this->data['page_data']=$page_arr;
				}
			}
			$this->load->view('welcome_message',$this->data);//渲染模板
			mysql_close($this->admin_conn);
		}

		 /**
	      * 保存权限节点
	      * @param 权限节点数组 $node
	      * @param 当前节点树的父节点集合 $rw
	      */
	     private function saveAccess($node,$rw){
	     	$arr=array();
	     	foreach ($node as $r){
	     		if(empty($rw)){
	     			$pid=0;
	     		}else{
	     			$pid=isset($rw[$r['pId']])?$rw[$r['pId']]:'';
	     			if(empty($pid))continue;
	     		}
	     		if($r['isNew']){//新增
	     			$log_arr	=	array(
	     					'pid'=>$pid,
	     					'access_name'=>$r['access_name'],
	     					'access_url'=>$r['access_url'],
	     					'target'=>$r['target'],
	     					'depth'=>$r['depth'],
	     					'access_order'=>$r['access_order'],
	     					'status'=>$r['status'],
	     			);
	     			$this->db->insert('cfd_admin_access', $log_arr);
	     			$arr[$r['id']]	=	$this->db->insert_id();//重新赋值ID
	     		}else{//编辑
	     			if($r['isClick']){//该节点被编辑过才进行保存
	     				$update_arr	=	array(
	     						'access_name'=>$r['access_name'],
	     						'access_url'=>$r['access_url'],
	     						'target'=>$r['target'],
	     						'access_order'=>$r['access_order'],
	     						'status'=>$r['status'],
	     				);
	     				$where=" sid='".$r['id']."' ";
						$this->db->where($where);
						$this->db->update('cfd_admin_access', $update_arr);
	     			}
	     			$arr[$r['id']]=$r['id'];//重新赋值ID
	     		}
	     	}
	     	return $arr;
	     }

		private function fisker_decode_v2($s){
	    	if($s=="")return $s;
	    	$a = str_split($s,2);
	    	$s = '%' . implode('%',$a);
	    	return urldecode($s);
	    }
}